package com.wei.czz.common.utils;

import com.wei.czz.common.constant.Constant;
import com.wei.czz.common.constant.EntityConstant;
import com.wei.czz.common.constant.RedisConstant;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-06-02 10:43:17
 * className: CacheKeyUtils 缓存键生成工具类
 * version: 1.0
 * description:
 */
public class CacheKeyUtils {

    /**
     * 获取登录用户缓存键
     * @param userId  用户主键
     * @return 缓存键
     */
    public static String getSecurityUserKey(Long userId) {
        return RedisConstant.STRING + Constant.SECURITY_USER + RedisConstant.SPLIT + userId;
    }

    /**
     * 获取验证码缓存键
     * @param account 账号
     * @param type    验证码类型
     * @return 缓存键
     */
    public static String getValidateCodeKey(String account, Integer type) {
        return RedisConstant.STRING + EntityConstant.VALIDATE_CODE + RedisConstant.SPLIT + account + RedisConstant.SPLIT
                + type;
    }

    /**
     * 获取短信发送记录缓存键
     * @param account 请求账号
     * @param type    短信类型
     * @return 缓存键
     */
    public static String getSmsLogKey(String account, Integer type) {
        return RedisConstant.STRING + EntityConstant.SMS_LOG + RedisConstant.SPLIT + account + RedisConstant.SPLIT + type;
    }

    /**
     * 获取页面参数缓存键
     * 调用该方法必须确保有请求用户
     * @return 缓存键
     */
    public static String getParamCacheKey(String entity) {
        // 获取请求用户主键
        Long optUserId = SecurityUtils.getUserId();

        return RedisConstant.STRING + entity + RedisConstant.PAGE_PARAM + optUserId;
    }

    /**
     * 获取redis hash结构键
     * @param entity 实体
     * @return hash结构键
     */
    public static String getRedisHashKey(String entity) {
        return RedisConstant.HASH + entity;
    }

    /**
     * 获取部门关联用户页面参数缓存键
     * 调用该方法必须确保有请求用户
     * @param deptId 部门主键
     * @return 缓存键
     */
    public static String getDeptUserParamCacheKey(Long deptId) {
        Long optUserId = SecurityUtils.getUserId();

        return RedisConstant.STRING + EntityConstant.DEPT_USER + RedisConstant.PAGE_PARAM + optUserId +
                RedisConstant.SPLIT + deptId;
    }

    /**
     * 获取mq消息处理串行执行加锁键
     * @param entity 实体
     * @param suffix 后缀
     * @return 加锁键
     */
    public static String getMqMessageLockKey(String entity, String suffix) {
        return RedisConstant.LOCK + Constant.MQ_MESSAGE + RedisConstant.SPLIT + entity + RedisConstant.SPLIT + suffix;
    }
}
